package org.blogspot.t0yv0.marcalog.util

import org.blogspot.t0yv0.marcalog.model._

object Util {

  def getFirstLetter(field: String) : Char = {
    field match {
      case null => '_'
      case ""   => '_'
      case s    => {
        var c = s.charAt(0)
        if (c.isLetter) {
          c.toLower
        } else {
          '_'
        }
      }
    }
  }

  private val isbnPattern = 
    java.util.regex.Pattern.compile("^[0-9]{10,13}$")

  def parseISBN(code: String) : Option[ISBN] = {
    code match {
      case null => None
      case _    => {
        if (isbnPattern.matcher(code).matches) {
          Some (new ISBN(code))
        } else {
          None
        }
      }
    }
  }

  def toCollection[T](items: Seq[T]) : java.util.Collection[T] = {
    import collection.JavaConversions._;
    items
  }

  def readCollection[T](c: java.util.Collection[T]) : Seq[T] = {
    import collection.JavaConversions._;
    c.toSeq
  }

  def seq[T](result: AnyRef) : Seq[T] =
    readCollection[T](result.asInstanceOf[java.util.Collection[T]]);

}
